<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('cuiqg');
	}

	public function index()
	{
		

		if(! $this->cuiqg->doCache('access_token')){

		 	redirect($this->cuiqg->getAuthorCode());
		}

		$this->load->view('home');
	}

	public function audio() {

		do{
			if($this->input->is_ajax_request()) {
		
				if(! $this->cuiqg->doCache('access_token')){

		 			$ret = array( 
		 				'err_no' => -1, 
		 				'err_msg' => '未获得授权', 
		 				'results' => array('redirect_uri' => $this->cuiqg->getAuthorCode()),
		 					);
		 			break;
				}
				$text = $this->input->post('text', true);

				$result = $this->cuiqg->getAudio($text, $this->cuiqg->doCache('access_token'));
				
				if($result['status'] == 'success') {

					$ret = array('err_no' => 0 , 'err_msg' => 'success', 'results' => array('audio' => $result['results']));
				}
				else
				{
					$ret = array('err_no' => -2, 'err_msg' => '生产失败');
				}
			}

		} while(0);

		$this->output->set_output(json_encode($ret));
	}
}
